package com.sanpu.kxe.business.main.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.contract.AdviceInitiateContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：法律咨询
 * Created by 薛勇军 on 2019/3/12.
 */

public class AdviceInitiatePresenter implements AdviceInitiateContract.Presenter {

    private AdviceInitiateContract.View mView;

    private CompositeSubscription mSubscription;

    private String currentType;

    public AdviceInitiatePresenter(AdviceInitiateContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void initViewData(String currentType) {
        this.currentType = currentType;
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            final String mAuthenState = centerOutput.getChkSts();
            if (!TextUtils.equals(CommonConfig.AUTHEN_PASS, centerOutput.getChkSts())) {
                String remark = null;
                if (TextUtils.equals(CommonConfig.AUTHEN_NOTSUB, centerOutput.getChkSts())) {
                    remark = "请先完成实名认证！";
                } else if (TextUtils.equals(CommonConfig.AUTHEN_AUDIT, centerOutput.getChkSts())) {
                    remark = "实名认证正在审核中，请稍候";
                } else if (TextUtils.equals(CommonConfig.AUTHEN_NOPASS, centerOutput.getChkSts())) {
                    remark = "实名认证审核失败，请重新提交审核通过后才能发起咨询";
                } else if (TextUtils.equals(CommonConfig.AUTHEN_NOCOMPLETION, centerOutput.getChkSts())) {
                    remark = "需要补全资料才能发起咨询";
                }

                if (!TextUtils.isEmpty(remark)) {
                    DialogUtil.getInstance().dialogNoTitletTwoBtnHaveCallBack(mView.getContext(),
                            remark, "取消", "确定",
                            new DialogViewListener() {
                                @Override
                                public void btnRightConfirm() {
                                    if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, mAuthenState)) {
                                        MainOpenProvider.getInstance().openCertificationActivity(mView.getContext());
                                        ((Activity) mView.getContext()).finish();
                                    }
                                }

                                @Override
                                public void btnLeftCancel() {
                                    ((Activity) mView.getContext()).finish();
                                }
                            });
                    return;
                }
            }
            mView.showDataView(centerOutput.getCrpNm(), centerOutput.getUserName());
        }
    }

    @Override
    public void conmitTask(String contact, String phone, String question) {
        if (TextUtils.isEmpty(contact)) {
            ToastUtil.showTextViewPrompt("联系人姓名不能为空！");
            return;
        }
        if (contact.length() == 1) {
            ToastUtil.showTextViewPrompt("请填写完整的联系人姓名！");
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showTextViewPrompt("手机号码不能为空！");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(phone)) {
            ToastUtil.showTextViewPrompt("手机号码输入不正确！");
            return;
        }
        if (TextUtils.isEmpty(question)) {
            ToastUtil.showTextViewPrompt("请描述您遇到的问题！");
            return;
        }
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance()
                .conmitTask(currentType, contact, phone, question)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        MainOpenProvider.getInstance().openAdviceListActivity(mView.getContext());
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
